package com.yanjr.common.persistence;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.MappedSuperclass;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

/**
 * Entity支持类
 * @author ThinkGem
 * @version 2013-01-15
 */
@MappedSuperclass
public abstract class BaseEntity<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 分表标识比如 _00 _01 _201401 ...  要带上完整的分表后缀
	 */
	private String subTableFlag = "";
	private String databaseRouterKey;
	private Map<String, Object> aliasFields = new HashMap<String, Object>();
//	protected QueryBuilder queryBuilder = null;

	public String getSubTableFlag() {
		return subTableFlag;
	}
	public void setSubTableFlag(String subTableFlag) {
		this.subTableFlag = subTableFlag;
	}
	
	public void setAliasField(String field, Object value) {
		aliasFields.put(field, value);
	}
	
	public Object getAliasFields(String field) {
		return aliasFields.get(field);
	}
	
	/**
	 * 设置数据库路由key
	 * @param key
	 */
	public void setDatabaseRouterKey(String key) {
		databaseRouterKey = key;
	}
	
	/**
	 * 获取数据库路由key，为空则采用默认规则
	 * @return
	 */
	public String getDatabaseRouterKey() {
		return databaseRouterKey;
	}
	
	/**
	 * 获取数据库名
	 * @return
	 */
	public String getDatabase() {
		return EntityContainer.getDatabase(this);
	}
	
	/**
	 * 获取实体影射实例
	 * @return
	 */
	public ParameterizedRowMapper<?> getRowMapper() {
		return EntityContainer.getRowMapper(this.getClass());
	}
	
	/**
	 * 获取表名
	 * @return
	 */
	public String getFullTableName() {
		return EntityContainer.getTable(this) + this.subTableFlag;
	}
	
//	public QueryBuilder getQueryBuilder() {
//		return this.queryBuilder;
//	}

	
	
	
}
